package com.cncbox.controller;

import com.cncbox.entity.ClickLink;
import com.cncbox.entity.NoteResult;
import com.cncbox.service.ClickLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 点击链接
 * Created by songs on 2016/4/13.
 */

@Controller
@RequestMapping(value = "/manager")
public class ClickLinkController {

    @Autowired
    private ClickLinkService clickLinkService;


    //添加数据
    @RequestMapping(value = "/addLink")
    public String addLink(HttpServletRequest httpServletRequest, ClickLink clickLink) throws Exception {
        NoteResult result = new NoteResult();
        String className = clickLink.getClassName();
        String clickPath = clickLink.getClickPath();
        String titleName = clickLink.getTitleName();

        if (className != null && clickPath != null && titleName != null) {
            result = clickLinkService.addClickLink(clickLink);
        } else {
            result.setMsg("PARAMETER_ERROR");
            result.setStatus(0);
        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/jump.html";
    }

    //查询数据根据分类
    @RequestMapping(value = "/listLink")
    @ResponseBody
    public NoteResult listLink(String className) throws Exception {
        NoteResult result = new NoteResult();
        if (className != null && !"".equals(className)) {
            result = clickLinkService.selectClickLink(className);
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        return result;
    }

    //修改数据前查询
    @RequestMapping(value = "/quLink")
    @ResponseBody
    public NoteResult listLink(Integer id) throws Exception {
        NoteResult result = new NoteResult();
        if (id != null && 0 < id) {
            result = clickLinkService.queryClickLink(id);
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        return result;
    }

    //修改数据
    @RequestMapping(value = "/upLink")
    public String ipLink(HttpServletRequest httpServletRequest, ClickLink clickLink) throws Exception {
        NoteResult result = new NoteResult();
        if (clickLink != null) {
            result = clickLinkService.updateClickLink(clickLink);
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/jump.html";
    }

    //删除数据
    @RequestMapping(value = "/delLink")
    public String delLink(HttpServletRequest httpServletRequest, Integer cid) throws Exception {
        NoteResult result = new NoteResult();
        if (cid != null && 0 < cid) {
            result = clickLinkService.deleteClickLink(cid);
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/jump.html";
    }
}
